/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */


package ru.teleology.cubefs.api.models;

import ru.teleology.cubefs.api.reflection.IDValue;
import ru.teleology.cubefs.api.reflection.NativeType;
import ru.teleology.cubefs.api.reflection.Type;
import ru.teleology.cubefs.api.reflection.TypeSystem;
import ru.teleology.cubefs.api.reflection.Typed;
import ru.teleology.cubefs.api.reflection.Types;
import ru.teleology.cubefs.api.reflection.metadata.ContainerMetadata;

@NativeType(name = Types.CONTAINER_TYPE)
public class Container extends Typed {

	public static final int STREAM 	= 0;
	public static final int FILE 		= 1;
	public static final int SHARED 	= 2;
	
	private static final Type myType = TypeSystem.find(Types.CONTAINER_TYPE);
	private static final StaticOperations staticOperations = (StaticOperations) myType.staticProxy();
	
	private Operations operations = (Operations) myType.proxy(this);
	
	public Container(long handle) {
		super(handle, false);
		this.GetManager().GetLogger().SetLogHandler(new BasicJavaLogHandler());
		this.GetManager().GetLogger().SetLogLevel(LogHandler.ERROR);
	}
	
	public static ContainerMetadata GetMetadata(int container_type) {
		return staticOperations.GetMetadata(container_type);
	}
	
	public static Container Create(int container_type) {
		return staticOperations.Create(container_type);
	}
	
	public int ContainerType() {
		return operations.ContainerType();
	}
	
	public Manager GetManager() {
		return operations.GetManager();
	}
	
	public TreeMap roots() {
		return operations.roots();
	}
	
	public Model GetCollection(IDValue rootId) {
		return operations.GetCollection(rootId);
	}
	
	public Model GetCollectionByName(long name) {
		return operations.GetCollectionByName(name);
	}
    
	public <T> T CreateCollection(int type, long name) {
		return (T)operations.CreateCollection(type, name);
    }
	
	public void ReleaseCollection(Model model) {
		operations.ReleaseCollection(model);
	}
    
	public void RemoveCollection(Model model) {
		operations.RemoveCollection(model);
	}

	public boolean Check() {
		return operations.Check();
	}
	
	public interface Operations {
		int ContainerType();
		Manager GetManager();
		TreeMap roots();
		Model GetCollection(IDValue rootId);
		Model GetCollectionByName(long name);
	    Model CreateCollection(int type, long name);
	    boolean Check();
	    
	    void ReleaseCollection(Model model);
	    void RemoveCollection(Model model);
	}
	
	public interface StaticOperations {
		ContainerMetadata GetMetadata(int container_type);
	    Container Create(int container_type);
	}
}
